﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace RestaurantTabletApplication
{
    class ClientMenu
    {
        private static volatile ClientMenu instance;
        private static object syncRoot = new Object();

        private List<Meal> meals = new List<Meal>();

        private ClientMenu()
        {
            populateMenuFromService();
        }

        public static ClientMenu Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new ClientMenu();
                        }
                    }
                }
                return instance;
            }
        }

        public List<Meal> Meals
        {
            get { return meals; }
        }

        public Meal getMealByNumber(int mealNumber)
        {
            foreach (Meal meal in meals)
            {
                if (meal.MealNumber == mealNumber)
                {
                    return meal;
                }
            }
            return null;
        }

        private void populateMenuFromService()
        {
            RestaurantWebService service = new RestaurantWebService();
            Meal[] serviceMeals = service.getMenu().Meals;
            foreach (Meal meal in serviceMeals)
            {
                meals.Add(meal);
            }
        }
    }
}
